<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_curriculum Extends MY_Model
{

	public function __construct()
	{
		parent::__construct();
	
	}
	
	function get_level_curriculum($id)
	{
		$sql = "SELECT c.subject_name,
					   c.subject_desc
				From curriculum c
				WHERE level_id = ?";
				
		$query = $this->db->query($sql,array($id));
		return $query->num_rows() >= 1 ? $query->result() : FALSE;
	}
	
	public function get_curriculum_by_level($level_id)
	{
		$sql = "SELECT *
				From curriculum c
				WHERE level_id = ?";
				
		$query = $this->db->query($sql,array($level_id));
		return $query->num_rows() >= 1 ? $query->result() : FALSE;
	}
	
	//new for curriculum
	public function get_subjects_available_for_curriculum($level_id)
	{
		$sql	 = 'SELECT  sub.subj_id,
							sub.subj_code,
							sub.subj_desc,
							grade_level_id
					FROM subjects sub
					LEFT JOIN curriculum cur ON cur.level_id = sub.grade_level_id
					WHERE sub.subj_id  NOT IN( SELECT subject_id from curriculum WHERE level_id = ? )
					AND sub.grade_level_id = ?
					GROUP BY sub.subj_id';
		$query = $this->db->query($sql,array($level_id,$level_id));
		return $query->result();
	}
	
	public function get_selected_subjects_for_curriculum($level_id)
	{
		$sql = "SELECT c.subject_name,
					   c.subject_desc,
					   c.subject_id,
					   c.id,
					   c.position as `order`,
					   c.learning_areas
				FROM curriculum c
				WHERE level_id = ?
				ORDER BY position,id DESC";
		$query = $this->db->query($sql,array($level_id));
		
		return $query->num_rows() >=1 ? $query->result() : FALSE;
	}
	
	public function set_curriculum_for_level($id,$data)
	{
		$data = implode(',',$data);
		$d_secure = $this->db->escape_str($data);
		$sql = "INSERT INTO curriculum(subject_name,subject_desc,subject_id,ay_id,level_id,created)
				SELECT subj_code,
					   subj_desc,
					   subj_id,
					   ?,?,?
				FROM subjects
				WHERE subj_id in ($d_secure)
				";
				
		$this->db->query($sql,array($this->c_sy->id,$id,NOW));
		return $this->db->affected_rows() >=1 ? TRUE : FALSE;
		
	}
	
	public function del_curriculum_for_level($subject_id,$level_id)
	{

		$this->db->where('subject_id', $subject_id);
		$this->db->where('level_id', $level_id);
		$this->db->where('ay_id', $this->c_sy->id);
		$this->db->delete('curriculum');
		return $this->db->affected_rows() >=1 ? TRUE : FALSE;
		
	}
	




	public function remove_from_curriculum($id)
	{
		$this->db->delete('curriculum', array('id' => $id)); 
		return $this->db->affected_rows() >=1 ? TRUE : FALSE;
	}

	public function set_order($order,$c_id)
	{

		$this->db->set('position',$order)->where('id',$c_id)->update('curriculum');
		return $this->db->affected_rows() >=1 ? TRUE : FALSE;
	}
	
	public function update_learning_areas($id,$option)
	{
		$this->db->set('learning_areas',$option)->where('id',$id)->update('curriculum');
		return $this->db->affected_rows() >=1 ? TRUE : FALSE;
	}
}